package Interface;

import Cards.Card;
import JPAController.CardJpaController;
import JPAController.exceptions.PreexistingEntityException;
import javax.swing.JOptionPane;

public class CardCreationScreen extends javax.swing.JFrame {

    /**
     * Creates new form CardCreationScreen
     */
    public CardCreationScreen() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labelCode = new javax.swing.JLabel();
        labelName = new javax.swing.JLabel();
        labelCategory = new javax.swing.JLabel();
        labelClan = new javax.swing.JLabel();
        labelCost = new javax.swing.JLabel();
        labelPower = new javax.swing.JLabel();
        labelDamage = new javax.swing.JLabel();
        labelEffect = new javax.swing.JLabel();
        labelP1 = new javax.swing.JLabel();
        labelP2 = new javax.swing.JLabel();
        fieldCode = new javax.swing.JTextField();
        fieldName = new javax.swing.JTextField();
        comboCategory = new javax.swing.JComboBox();
        fieldClan = new javax.swing.JTextField();
        fieldCost = new javax.swing.JTextField();
        fieldPower = new javax.swing.JTextField();
        fieldDamage = new javax.swing.JTextField();
        comboEffect = new javax.swing.JComboBox();
        fieldP1 = new javax.swing.JTextField();
        fieldP2 = new javax.swing.JTextField();
        buttonSave = new javax.swing.JButton();
        buttonClean = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        labelCode.setText("Código");

        labelName.setText("Nome");

        labelCategory.setText("Categoria");

        labelClan.setText("Clã");

        labelCost.setText("Custo");

        labelPower.setText("Poder");

        labelDamage.setText("Dano");

        labelEffect.setText("Efeito");

        labelP1.setText("Parâmetro 1");

        labelP2.setText("Parâmetro 2");

        comboCategory.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Humano", "Místico", "Fera", "Espírito", "Combinação" }));

        comboEffect.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Sem Efeito", "Dano e Cura", "Dano por Combinação", "Dano por Combinação e Poder", "Cura por Combinação", "Cura por Combinação e Poder", "Cortar Ataque por Combinação", "Cortar Ataque por Combinação e Poder", "Cortar Ataque por Rivalidade", "Cortar Ataque por Rivalidade e Poder" }));

        buttonSave.setText("Salvar");
        buttonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSaveActionPerformed(evt);
            }
        });

        buttonClean.setText("Limpar");
        buttonClean.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCleanActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelCode)
                            .addComponent(labelName)
                            .addComponent(labelCost)
                            .addComponent(labelPower))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelP1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(fieldP1))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(labelCategory)
                                    .addComponent(labelClan)
                                    .addComponent(labelDamage)
                                    .addComponent(labelEffect))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(fieldCost)
                                    .addComponent(comboCategory, 0, 305, Short.MAX_VALUE)
                                    .addComponent(fieldName)
                                    .addComponent(fieldClan)
                                    .addComponent(fieldPower)
                                    .addComponent(fieldDamage)
                                    .addComponent(fieldCode)
                                    .addComponent(comboEffect, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(labelP2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(buttonClean)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 181, Short.MAX_VALUE)
                                        .addComponent(buttonSave))
                                    .addComponent(fieldP2))))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelCode)
                    .addComponent(fieldCode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelName)
                    .addComponent(fieldName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelCategory)
                    .addComponent(comboCategory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelClan)
                    .addComponent(fieldClan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelCost)
                    .addComponent(fieldCost, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelPower)
                    .addComponent(fieldPower, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelDamage)
                    .addComponent(fieldDamage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelEffect)
                    .addComponent(comboEffect, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelP1)
                    .addComponent(fieldP1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelP2)
                    .addComponent(fieldP2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonSave)
                    .addComponent(buttonClean))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private boolean isAllFalse(){
        if(this.fieldClan.getText().equals("") == false ||
           this.fieldCode.getText().equals("") == false ||
           this.fieldCost.getText().equals("") == false ||
           this.fieldDamage.getText().equals("") == false ||
           this.fieldName.getText().equals("") == false ||
           this.fieldPower.getText().equals("") == false){
            return true;
        }
        return false;
    }
    
    private Card mountCard(){
        Card c = new Card();
        c.setActualDamage(Integer.parseInt(this.fieldDamage.getText()));
        c.setClan(this.fieldClan.getText());
        c.setCode(this.fieldCode.getText());
        c.setCost(Integer.parseInt(this.fieldCost.getText()));
        c.setCurrentDamage(c.getActualDamage());
        c.getEffect().setCategory(this.comboEffect.getSelectedIndex() + "");
        c.getEffect().setParameter1(this.fieldP1.getText());
        c.getEffect().setParameter2(this.fieldP2.getText());
        c.setName(this.fieldName.getText());
        c.setPower(Integer.parseInt(this.fieldPower.getText()));
        c.setType((String) this.comboCategory.getSelectedItem());
        return c;
    }
    
    private void buttonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSaveActionPerformed
        // TODO add your handling code here:
        if(isAllFalse()){
            CardJpaController jpa = new CardJpaController();
            try {
                jpa.create(mountCard());
            } catch (PreexistingEntityException ex) {
                JOptionPane.showMessageDialog(null, "Já existe essa tupla.");
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(null, "Problema com o banco de dados.");
            }
        } else {
            JOptionPane.showMessageDialog(null, "Verifique o preenchimento dos campos.");
        }
    }//GEN-LAST:event_buttonSaveActionPerformed

    private void buttonCleanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCleanActionPerformed
        this.comboCategory.setSelectedIndex(0);
        this.comboEffect.setSelectedIndex(0);
        this.fieldClan.setText("");
        this.fieldCode.setText("");
        this.fieldCost.setText("");
        this.fieldDamage.setText("");
        this.fieldName.setText("");
        this.fieldP1.setText("");
        this.fieldP2.setText("");
        this.fieldPower.setText("");
    }//GEN-LAST:event_buttonCleanActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(CardCreationScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(CardCreationScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(CardCreationScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(CardCreationScreen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CardCreationScreen().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonClean;
    private javax.swing.JButton buttonSave;
    private javax.swing.JComboBox comboCategory;
    private javax.swing.JComboBox comboEffect;
    private javax.swing.JTextField fieldClan;
    private javax.swing.JTextField fieldCode;
    private javax.swing.JTextField fieldCost;
    private javax.swing.JTextField fieldDamage;
    private javax.swing.JTextField fieldName;
    private javax.swing.JTextField fieldP1;
    private javax.swing.JTextField fieldP2;
    private javax.swing.JTextField fieldPower;
    private javax.swing.JLabel labelCategory;
    private javax.swing.JLabel labelClan;
    private javax.swing.JLabel labelCode;
    private javax.swing.JLabel labelCost;
    private javax.swing.JLabel labelDamage;
    private javax.swing.JLabel labelEffect;
    private javax.swing.JLabel labelName;
    private javax.swing.JLabel labelP1;
    private javax.swing.JLabel labelP2;
    private javax.swing.JLabel labelPower;
    // End of variables declaration//GEN-END:variables
}
